//
//  SEGuideDiagnosisInfoConfirmView.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/8.
//

import UIKit

@IBDesignable class SEGuideDiagnosisInfoConfirmView: UIView {
    
    var agreeClosure: ((Bool) -> Void)?
    var confirmClosure: (() -> Void)?
    
    var view: UIView!

    @IBOutlet private weak var noteView: UIView! {
        didSet {
            noteView.setLayerCorners(rect: noteView.bounds, radius: 4)
        }
    }
    
    @IBOutlet private weak var confirmButton: UIButton! {
        didSet {
            confirmButton.setLayerCorners(rect: confirmButton.bounds,
                                          radius: confirmButton.height / 2)
        }
    }
    
    func updateUI(_ isHidden: Bool) {
        noteView.alpha = isHidden ? 0 : 1
    }
    
    // MARK: - action event
    
    @IBAction private func agreeAction(_ sender: UIButton) {
        sender.isSelected.toggle()
        agreeClosure?(sender.isSelected)
    }
    
    @IBAction private func confirmAction() {
        confirmClosure?()
    }
    
    // MARK: - life cycle
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI()
    }
    
    private func setupUI() {
        view = loadViewFromNib()
        view.backgroundColor = .white
        view.setLayerCorners(rect: view.bounds, radius: 16, corners: [.topLeft, .topRight])
    }
    
    // MARK: - load view from xib
    
    private func loadViewFromNib() -> UIView {
        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: nibName(), bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView

        view.frame = bounds
        view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        addSubview(view)

        return view
    }

    private func nibName() -> String {
        return String(describing: type(of: self))
    }
}
